{***************************************************************
    SpyGestion  Copyright (C) 2008 Christophe KOALI <nature2gas@gmail.com>
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
****************************************************************}
unit modLangGetList;

interface

uses modLanguageParsing, ScruteX, Classes, SysUtils;


type
    TLangResult = record
        Fichier: string;
        Nom    : string;
    end;

    PLangResult = ^TLangResult;

    
type
    TScanLanguage = class(TList)
    private
       FScanner : TScruteDossier;
       FLangParsing: TParsingLanguage;

       procedure OnFileFound(Sender: TObject; Nom: string);
    public
       procedure ScanDir(Dossier : string);

         constructor Create;
         destructor Destroy; override;

         function Delete(Index: integer): boolean;
         function Add(Fichier, NomCombo: string): PLangResult;
         procedure SimpleAdd(Pointeur: PLangResult);
         procedure Clear; override;

    end;



implementation

// Simple ajout de pointeur
procedure TScanLanguage.SimpleAdd(Pointeur: PLangResult);
begin
    inherited Add(Pointeur);  // Ajoute le pointeur à la liste
end;

function TScanLanguage.Delete(Index: integer): boolean;
var pElement: PLangResult;
begin

    Result := false;

    if (Index < 0) or (Index >= Count) then
       // Hors limite
       exit;


    // Obtient le pointeur
    pElement := Items[Index];

    if pElement <> nil then
       // Libére la mémoire créée lors de l'ajout
       Dispose(pElement);

    // Supprime le pointeur de la liste
    inherited Delete(Index);

    // Met le resultat à True
    Result := true;

end;

procedure TScanLanguage.Clear;
var i: integer;
begin

    if Count > 0 then begin
        For i := Count -1 downto 0 do
            Delete(i);
    end;

    inherited Clear;
end;

function TScanLanguage.Add(Fichier, NomCombo: string): PLangResult;
begin

    New(Result);          // Créer l'espace mémoire requis
    Result.Fichier := Fichier;
    Result.Nom     := NomCombo;


    try
       inherited Add(Result);  // Ajoute le pointeur à la liste
    except
       Result := nil;
    end;
    
    // Pour libérer la mémoire occupé par l'élément pointer:  Dispose(pElement);

end;


procedure TScanLanguage.OnFileFound(Sender: TObject; Nom: string);
begin

    if FLangParsing.LoadFromFile(Nom, True) > 0 then begin
       Add(Nom, FLangParsing.NomCombo);
    end;

end;

procedure TScanLanguage.ScanDir(Dossier : string);
begin
    Clear;
    FScanner.Dossier := Dossier;

    FScanner.Execute;
end;

constructor TScanLanguage.Create;
begin
    FScanner := TScruteDossier.Create(nil);
    FScanner.OnFichier := OnFileFound;
    FScanner.Filtre := '*.ini';
    FScanner.SousDossier := false;
    FScanner.MaxPenetration := 1;

    FLangParsing := TParsingLanguage.Create;

end;


destructor TScanLanguage.Destroy;
begin
    FScanner.Free;
    FLangParsing.Free;

    inherited Destroy;
end;


end.
